package com.settlement.system.service.impl;

import com.settlement.system.model.entity.BsOmsOrderDetail;
import com.settlement.system.mapper.BsOmsOrderDetailMapper;
import com.settlement.system.service.BsOmsOrderDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.BsOmsOrderDetailQuery;
import com.settlement.system.model.form.BsOmsOrderDetailForm;
import org.springframework.beans.BeanUtils;
import com.settlement.system.model.vo.BsOmsOrderDetailImportVo;

/**
 * <p>
 * 百盛订单明细表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-09-04
 */
@Service
public class BsOmsOrderDetailServiceImpl extends ServiceImpl<BsOmsOrderDetailMapper, BsOmsOrderDetail> implements BsOmsOrderDetailService {

    @Override
    public Page<BsOmsOrderDetail> getPage(BsOmsOrderDetailQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<BsOmsOrderDetail> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveBsOmsOrderDetail(BsOmsOrderDetailForm bsOmsOrderDetailForm) {
         BsOmsOrderDetail bsOmsOrderDetail = new BsOmsOrderDetail();
         BeanUtils.copyProperties(bsOmsOrderDetailForm, bsOmsOrderDetail);
         bsOmsOrderDetail.setCreateTime(null);
         return this.saveOrUpdate(bsOmsOrderDetail);
     }

     @Override
     public BsOmsOrderDetailForm getBsOmsOrderDetailFormData(Long id) {
         BsOmsOrderDetail bsOmsOrderDetail = this.getById(id);
         BsOmsOrderDetailForm bsOmsOrderDetailForm = new BsOmsOrderDetailForm();
         BeanUtils.copyProperties(bsOmsOrderDetail, bsOmsOrderDetailForm);
         return bsOmsOrderDetailForm;
     }

     @Override
     public boolean deleteBsOmsOrderDetail(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<BsOmsOrderDetailImportVo> voList)  {
        List<BsOmsOrderDetail> bsOmsOrderDetailList = new ArrayList<>();
        for (BsOmsOrderDetailImportVo importVo : voList) {
            BsOmsOrderDetail bsOmsOrderDetail = new BsOmsOrderDetail();
            BeanUtils.copyProperties(importVo, bsOmsOrderDetail);
            bsOmsOrderDetailList.add(bsOmsOrderDetail);
        }
        this.saveOrUpdateBatch(bsOmsOrderDetailList);
    }

    @Override
    public List<BsOmsOrderDetailImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<BsOmsOrderDetail> bsOmsOrderDetailList = this.getBaseMapper().selectBatchIds(ids);
        List<BsOmsOrderDetailImportVo> voList = new ArrayList<>();
        for (BsOmsOrderDetail bsOmsOrderDetail : bsOmsOrderDetailList) {
            BsOmsOrderDetailImportVo vo = new BsOmsOrderDetailImportVo();
            BeanUtils.copyProperties(bsOmsOrderDetail, vo);
            voList.add(vo);
        }
        return voList;
    }
}
